<?php
/**
 * Created by JetBrains PhpStorm.
 * User: CAIXUDONG
 * Date: 11-6-20
 * Time: 下午4:17
 */
 
class RoleController extends Controller{

    public function index(){

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);

        $list = Model::getInstance('role')->getRoleList($curr_page,$per_page);
        $data = array(
            'list'=>$list,
        );
        Template::show('admin/role.php',$data);
    }

    public function lookup(){

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);

        $list = Model::getInstance('role')->getRoleList($curr_page,$per_page);

        $parent_rel  = $this->input()->get('rel');
        $data = array(
            'parent_rel'=>$parent_rel,
            'list'=>$list,
        );
        Template::show('admin/role_lookup.php',$data);
    }

    public function add(){

        Template::show('admin/add_role.php');
    }

    public function del(){

        $role_id = $this->input()->get('role_id');
        $ret = Model::getInstance('role')->delRole($role_id);

        if($ret){
            Model::getInstance('catehasrole')->delByRoleId($role_id);
            Model::getInstance('userhasrole')->delByRoleId($role_id);
            $data = array(
                "statusCode"=>"200",
                "navTabId"=>"role",
            );
            echo Response::JSON($data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON($data);
        }
    }

    public function edit(){

        $role_id = (int)$this->input()->get('role_id');

        $role_info = Model::getInstance('role')->get($role_id);
        $data = array(
            'role_info'=>$role_info,
        );

        Template::show('admin/edit_role.php',$data);
    }

    public function set(){
        
        $role_id = (int)$this->input()->get('role_id');

        $roles = Model::getInstance('role')->getRoleCates($role_id);

        $cate_ids = array();
        foreach($roles as $v){
            $cate_ids[] = $v['cate_id'];
        }

        $menu_arr = Model::getInstance('cate')->getTree();
        
        $data = array(
            'role_id'=>$role_id,
            'menu_arr'=>$menu_arr,
            'cate_ids'=>$cate_ids,
        );
        
        Template::show('admin/role_cate_tree.php',$data);
    }
}